package com.zjut.spider;

import com.zjut.dao.ProductMapper;
import com.zjut.seleniumdownloader.SeleniumDownloaderX;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;

import javax.annotation.Resource;

/**
 * Created by Ryan on 2016/10/2.
 */
public class SpiderDemo {

    private static ProductMapper productMapper;

    private static Logger logger = Logger.getLogger(SpiderDemo.class);
    static
    {
        ApplicationContext applicationContext= new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");
        productMapper = (ProductMapper) (applicationContext.getBean("productMapper"));
    }

    public static void main(String[] args){

      /*  Map<String, Object> contentSettings = new HashMap<String, Object>();
        contentSettings.put("images", 2);
        Map<String, Object> preferences = new HashMap<String, Object>();
        preferences.put("profile.default_content_settings", contentSettings);

        DesiredCapabilities caps = DesiredCapabilities.chrome();
        caps.setCapability("chrome.prefs", preferences);
        WebDriver driver = new ChromeDriver(caps);*/


        SeleniumDownloaderX seleniumDownloader = new SeleniumDownloaderX("D:\\webmagic\\chromedriver.exe");
        seleniumDownloader.setThread(1);
        seleniumDownloader.setSleepTime(100);
        long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
        Spider.create(new JDDBDProcessor()).addUrl(JDDBDProcessor.URL_FIRST)
//                .addPipeline(new JsonFilePipeline("D:\\webmagic\\"))
                .addPipeline(new MyBatisPipeline4JDDBD(productMapper))
                .addPipeline(new ConsolePipeline())
                .thread(1)
//                .setDownloader(seleniumDownloader)
                .run();
        long endMili=System.currentTimeMillis();
        System.out.println("总耗时为："+(endMili-startMili)/1000.0+"秒");
    }
}
